/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nl.hanze.web.ola;

import java.util.HashMap;

/**
 *
 * @author Luuk
 */
public class AcceptGiroValidator {
    
    private HashMap<String, String> data;
    
    public AcceptGiroValidator(HashMap data){
        this.data = data;
    }
    
    public boolean validate(){
        try{
            return checkInteger(data.get("reference"), 0, 999999999) 
                    && checkInteger(data.get("euro"), 1, 99999)
                    && checkInteger(data.get("cent"), 0, 99)
                    && checkStringLength(data.get("betalingskenmerk"), 16, 16, true)
                    && checkStringLength(data.get("rekeningnummer"), 1, 10, false)
                    && checkStringLength(data.get("init"), 1, 5, true)
                    && checkStringLength(data.get("achternaam"), 1, 20, true)
                    && checkStringLength(data.get("straatnaam"), 1, 20, true)
                    && checkInteger(data.get("straatnummer"), 1, 99999)
                    && checkStringLength(data.get("plaatsnaam"), 1, 20, true)
                    && checkStringLength(data.get("naamnaar"), 1, 20, true)
                    && checkPostalCode(data.get("postcode"))
                    && checkStringLength(data.get("rekeningnummernaar"), 1, 10, true);
        } catch (Exception e){
            return false;
        }
    }
    
    public boolean checkStringLength(String testString, int min, int max, boolean required){
        if(testString.length() >= min && testString.length() <= max){
            return true;
        }else{
            if(required){
                return false;
            }else{
                return true;
            }
        }
    }
    
    public boolean checkInteger(String testString, int min, int max){
        int testInt = Integer.parseInt(testString);
        return testInt >= min && testInt <= max;
    }
    
    public boolean checkPostalCode(String testString){
        for(int i = 0; i < 6; i++){
            char c = testString.charAt(i);
            if(!Character.isDigit(c) && i < 4){
                return false; 
            }
            if(!Character.isLetter(c) && i < 6 && i > 3){
                return false;
            }
        }
        return true;
    }
    
}
